<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <common-item1
          :query="query"
          :streetList="streetList"
          :communityList="communityList"
          :villageList="villageList"
          @visibleChangeStreet="visibleChangeStreet"
          @changeStreet="changeStreet"
          @visibleChangeCommunity="visibleChangeCommunity"
          @changeCommunity="changeCommunity"
          @visibleChangeVillage="visibleChangeVillage"
          @changeVillage="changeVillage"
          :show="false"
          :showInput="showInput"
        />
        <template>
          <el-radio
            :disabled="disabled"
            v-model="query.status"
            label="1"
            @click.native.prevent="single('1')"
            >昨日参与率</el-radio
          >
          <el-radio
            :disabled="disabled"
            v-model="query.status"
            label="2"
            @click.native.prevent="single('2')"
            >上周参与率</el-radio
          >
          <el-radio
            :disabled="disabled"
            v-model="query.status"
            label="3"
            @click.native.prevent="single('3')"
            >当月参与率</el-radio
          >
        </template>

        <div class="block" style="display: inline-block; margin-right: 10px;">
          <el-date-picker
            v-model="value2"
            type="daterange"
            align="left"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :readonly="readonly"
            @focus="onfocus"
          >
          </el-date-picker>
        </div>

        <el-button type="primary" @click="handleSearch1">查询参与</el-button>
        <el-button type="primary" @click="handleSearch2">查询未参与</el-button>

        <el-button type="primary" @click="handleExport1">导出参与</el-button>
        <el-button type="primary" @click="handleExport2">导出未参与</el-button>
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
      >
        <el-table-column
          prop="index"
          label="排序"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="subDistrict"
          label="街道"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="communityName"
          label="社区"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="villageName"
          label="小区"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="buildingNo"
          label="楼号"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="unitNo"
          label="单元号"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="houseNumber"
          label="门牌号"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="rfid"
          label="芯片编号"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="qrCode"
          label="二维码"
        ></el-table-column>

        <el-table-column
          v-if="show1"
          align="center"
          prop="garbageWeight"
          label="垃圾总重量"
        ></el-table-column>

        <el-table-column v-if="show1" label="参与次数" align="center">
          <template slot-scope="scope">{{ scope.row.frequency }}</template>
        </el-table-column>

        <el-table-column v-if="show2" label="是否空置">
          <template slot-scope="scope">{{
            scope.row.enableStatus == "0" ? "空置" : "非空置"
          }}</template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total,sizes, prev, pager, next, jumper"
          :page-sizes="[5, 10, 20, 30]"
          @size-change="handleSizeChange"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import NProgress from "nprogress";
import { commonItem1 } from "../../common/commonItem1/common-item1.js";
export default {
  name: "YesNoPartHouseManage",
  data() {
    return {
      query: {
        url: "",
        streetId: "",
        communityId: "",
        villageId: "",
        status: "",
        streetName: "",
        communityName: "",
        villageName: "",
        pageIndex: 1,
        pageSize: 20,
      },
      showInput: true,
      show1: false,
      show2: false,
      value2: "",
      streetList: [],
      communityList: [],
      villageList: [],
      tableData: [],
      readonly: false,
      disabled: false,

      editVisible: false,
      pageTotal: 0,
    };
  },
  created() {
    // this.getData();
  },
  components: {
    "common-item1": () => import("../../common/commonItem1/common-item1.vue"),
  },
  methods: {
    single(num) {
      console.log(num);
      this.disabled = false;
      this.readonly = true;
      this.value2 = "";
      this.query.status = num;
    },
    onfocus() {
      console.log(222);
      this.disabled = true;
      this.readonly = false;
      this.query.status = "";
    },
    ...commonItem1,
    // 获取 easy-mock 的模拟数据
    getData(url, params) {
      if (this.query.villageId === "" && this.query.villageName.trim() == "") {
        return this.$message.warning({
          message: "请选择查询的小区",
          duration: 1500,
        });
      }

      if (this.query.status === "" && this.value2 === "") {
        return this.$message.warning({
          message: "请选择查询的时间",
          duration: 1500,
        });
      }
      NProgress.start();
      this.$axios
        .get(url, {
          params: {
            ...params,
            villageName:
              this.showInput == false ? "" : this.query.villageName.trim(),
          },
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            NProgress.done();
            return this.$message.error(res.msg);
          }
          this.tableData = res.data.list;
          this.pageTotal = res.data.total || 0;
          this.tableData.map((item, index) => {
            var pageIndex = this.query.pageIndex;
            item.index = index + 1 + (pageIndex - 1) * this.query.pageSize;
          });
          NProgress.done();
          console.log(res);
        })
        .catch((err) => {});
    },
    // 查询参与
    handleSearch1() {
      this.show1 = true;
      this.show2 = false;
      this.$set(this.query, "pageIndex", 1);
      this.$set(
        this.query,
        "url",
        "/village/houseParticipation/pageParticipation"
      );

      this.getData(this.query.url, {
        page: this.query.pageIndex,
        limit: this.query.pageSize,
        villageId: this.query.villageId,
        status: this.query.status,
        startDate: this.value2 ? this.value2[0].getTime() : "",
        endDate: this.value2
          ? (this.value2[1].getTime() + (24 * 3600 * 1000 - 1000)).toString()
          : "",
      });
    },
    // 查询未参与
    handleSearch2() {
      this.show1 = false;
      this.show2 = true;
      this.$set(this.query, "pageIndex", 1);
      this.$set(
        this.query,
        "url",
        "/village/houseParticipation/pageNotParticipation"
      );
      this.getData(this.query.url, {
        page: this.query.pageIndex,
        limit: this.query.pageSize,
        villageId: this.query.villageId,
        status: this.query.status,
        startDate: this.value2 ? this.value2[0].getTime() : "",

        endDate: this.value2
          ? (this.value2[1].getTime() + (24 * 3600 * 1000 - 1000)).toString()
          : "",
      });
    },
    // 导出参与
    handleExport1() {
      this.export(
        "/village/houseParticipation/exportParticipation",
        {
          villageId: this.query.villageId,
          status: this.query.status,
          startDate: this.value2 ? this.value2[0].getTime() : "",
          endDate: this.value2
            ? (this.value2[1].getTime() + (24 * 3600 * 1000 - 1000)).toString()
            : "",
        },
        "参与户室"
      );
    },
    // 导出未参与
    handleExport2() {
      this.export(
        "/village/houseParticipation/exportNotParticipation",
        {
          villageId: this.query.villageId,
          status: this.query.status,
          startDate: this.value2 ? this.value2[0].getTime() : "",
          endDate: this.value2
            ? (this.value2[1].getTime() + (24 * 3600 * 1000 - 1000)).toString()
            : "",
        },
        "未参与户室"
      );
    },
    export(url, params, typeName = " ") {
      if (this.query.villageId === "") {
        return this.$message.warning({
          message: "请选择导出的小区",
          duration: 1500,
        });
      }

      if (this.query.status === "" && this.value2 === "") {
        return this.$message.warning({
          message: "请选择导出的时间",
          duration: 1500,
        });
      }
      NProgress.start();
      this.$axios
        .get(url, {
          params: {
            ...params,
          },
          responseType: "blob",
        })
        .then((res) => {
          const link = document.createElement("a");
          let blob = new Blob([res.data], { type: "application/vnd.ms-excel" });
          link.style.display = "none";
          link.href = URL.createObjectURL(blob);

          link.setAttribute(
            "download",
            `${this.query.villageName}_${typeName}.xls`
          );

          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
          NProgress.done();
        })
        .catch((err) => {});
    },
    // 删除操作
    handleDelete(index, row) {
      // 二次确认删除
      this.$confirm("确定要删除吗？", "提示", {
        type: "warning",
      })
        .then(() => {
          this.$message.success("删除成功");
          this.tableData.splice(index, 1);
        })
        .catch(() => {});
    },

    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, "pageIndex", val);
      this.getData(this.query.url, {
        page: this.query.pageIndex,
        limit: this.query.pageSize,
        villageId: this.query.villageId,
        status: this.query.status,
        startDate: this.value2 ? this.value2[0].getTime() : "",
        endDate: this.value2
          ? (this.value2[1].getTime() + (24 * 3600 * 1000 - 1000)).toString()
          : "",
      });
    },
    handleSizeChange(val) {
      this.$set(this.query, "pageSize", val);
      this.getData(this.query.url, {
        page: this.query.pageIndex,
        limit: this.query.pageSize,
        villageId: this.query.villageId,
        status: this.query.status,
        startDate: this.value2 ? this.value2[0].getTime() : "",
        endDate: this.value2
          ? (this.value2[1].getTime() + (24 * 3600 * 1000 - 1000)).toString()
          : "",
      });
    },
  },
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
